package com.zcc.makingfriend.service.impl;

import com.zcc.makingfriend.dao.TbUserConcerned;
import com.zcc.makingfriend.dao.User;
import com.zcc.makingfriend.mapper.TbUserConcernedMapper;
import com.zcc.makingfriend.mapper.UserMapper;
import com.zcc.makingfriend.service.TbUserConcernedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zcc
 */
@Service
public class TbUserConcernedServiceImpl implements TbUserConcernedService {

    @Autowired
    TbUserConcernedMapper tbUserConcernedMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public int addTbUserConcerned(TbUserConcerned tbUserConcerned) {
        return tbUserConcernedMapper.addTbUserConcerned(tbUserConcerned);
    }

    @Override
    public int deleteTbUserConcerned(Long userId, Long concernedId) {
        return tbUserConcernedMapper.deleteTbUserConcerned(userId, concernedId);
    }

    @Override
    public int getFanNumbers(Long concernedId) {
        return tbUserConcernedMapper.getFanNumbers(concernedId);
    }

    @Override
    public int getConcernedNumbers(Long userId) {
        return tbUserConcernedMapper.getConcernedNumbers(userId);
    }

    @Override
    public List<User> getFans(Long concernedId) {
        List<TbUserConcerned> fans = tbUserConcernedMapper.getFans(concernedId);
        List<User> users = new ArrayList<>();
        for (TbUserConcerned tbUserConcerned : fans) {
            User userByUuid = userMapper.findUserById(tbUserConcerned.getUserId());
            users.add(userByUuid);
        }
        return users;
    }

    @Override
    public List<User> getConcerneds(Long userId) {
        List<TbUserConcerned> concerneds = tbUserConcernedMapper.getConcerneds(userId);
        List<User> users = new ArrayList<>();
        for (TbUserConcerned t: concerneds) {
            User userByUuid = userMapper.findUserById(t.getConcernedId());
            users.add(userByUuid);
        }
        return users;
    }

    @Override
    public boolean isConcerned(Long userId, Long concernedId) {
        TbUserConcerned tbUserConcerned = tbUserConcernedMapper.isConcerned(userId, concernedId);
        return tbUserConcerned != null;
    }
}
